package com.example.java_gobang2.model;

import lombok.Data;

import java.util.UUID;

@Data
public class Room {
    private String roomId;
    private User user1;
    private User user2;
    private static final int ROW_MAX = 15;
    private static final int COL_MAX = 15;
    private int[][] chessBoard = new int[ROW_MAX][COL_MAX];

    public Room() {
        roomId = UUID.randomUUID().toString();
    }

    public boolean setChess(int row,int col,int chess) {
        if (chessBoard[row][col] == 0) {
            chessBoard[row][col] = chess;
            return true;
        }
        return false;
    }
    public int checkWinner(int chess, int row, int col) {
        boolean done = false;
// 1. 检查所有的⾏(循环五次)
        for (int c = col - 4; c <= col; c++) {
            if (c < 0 || c >= COL_MAX) {
                continue;
            }
            if (chessBoard[row][c] == chess
                    && chessBoard[row][c + 1] == chess
                    && chessBoard[row][c + 2] == chess
                    && chessBoard[row][c + 3] == chess
                    && chessBoard[row][c + 4] == chess) {
                done = true;
            }
        }
// 2. 检查所有的列(循环五次)
        for (int r = row - 4; r <= row; r++) {
            if (r < 0 || r >= ROW_MAX) {
                continue;
            }
            if (chessBoard[r][col] == chess
                    && chessBoard[r + 1][col] == chess
                    && chessBoard[r + 2][col] == chess
                    && chessBoard[r + 3][col] == chess
                    && chessBoard[r + 4][col] == chess) {
                done = true;
            }
        }
// 3. 检查左对⻆线
        for (int r = row - 4, c = col - 4; r <= row && c <= col; r++, c++) {
            if (r < 0 || r >= ROW_MAX || c < 0 || c >= COL_MAX) {
                continue;
            }
            if (chessBoard[r][c] == chess
                    && chessBoard[r + 1][c + 1] == chess
                    && chessBoard[r + 2][c + 2] == chess
                    && chessBoard[r + 3][c + 3] == chess
                    && chessBoard[r + 4][c + 4] == chess) {
                done = true;
            }
        }
// 4. 检查右对⻆线
        for (int r = row - 4, c = col + 4; r <= row && c >= col; r++, c--) {
            if (r < 0 || r >= ROW_MAX || c < 0 || c >= COL_MAX) {
                continue;
            }
            if (chessBoard[r][c] == chess
                    && chessBoard[r + 1][c - 1] == chess
                    && chessBoard[r + 2][c - 2] == chess
                    && chessBoard[r + 3][c - 3] == chess
                    && chessBoard[r + 4][c - 4] == chess) {
                done = true;
            }
        }
        if (!done) {
            return 0;
        }
        return chess == 1 ? user1.getUserId() : user2.getUserId();
    }

    public String ToStr() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ROW_MAX; i++) {
            for (int j = 0; j < COL_MAX; j++) {
                sb.append(chessBoard[i][j]).append(" ");
            }
            // 在每行末尾添加换行符，除了最后一行
            if (i < ROW_MAX - 1) {
                sb.append(System.lineSeparator());
            }
        }
        return sb.toString();
    }
}
